package com.gxp.system.service.impl;

import com.gxp.common.core.text.Convert;
import com.gxp.common.utils.DateUtils;
import com.gxp.common.utils.ShiroUtils;
import com.gxp.common.utils.StringUtils;
import com.gxp.system.domain.ZknjFavourite;
import com.gxp.system.domain.ZknjRequest;
import com.gxp.system.mapper.ZknjFavouriteMapper;
import com.gxp.system.mapper.ZknjMethodMapper;
import com.gxp.system.service.IZknjFavouriteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 收藏夹Service业务层处理
 * 
 * @author zh
 * @date 2024-08-20
 */
@Service
public class ZknjFavouriteServiceImpl implements IZknjFavouriteService 
{
    @Autowired
    private ZknjFavouriteMapper zknjFavouriteMapper;
    @Autowired
    private ZknjMethodMapper methodMapper;

    /**
     * 查询收藏夹
     * 
     * @param id 收藏夹主键
     * @return 收藏夹
     */
    @Override
    public ZknjFavourite selectZknjFavouriteById(Long id)
    {
        return zknjFavouriteMapper.selectZknjFavouriteById(id);
    }

    /**
     * 查询收藏夹列表
     * 
     * @param zknjFavourite 收藏夹
     * @return 收藏夹
     */
    @Override
    public List<ZknjFavourite> selectZknjFavouriteList(ZknjFavourite zknjFavourite)
    {
        return zknjFavouriteMapper.selectZknjFavouriteList(zknjFavourite);
    }

    /**
     * 新增收藏夹
     * 
     * @param zknjFavourite 收藏夹
     * @return 结果
     */
    @Override
    public int insertZknjFavourite(ZknjFavourite zknjFavourite)
    {
        zknjFavourite.setCreateTime(DateUtils.getNowDate());
        return zknjFavouriteMapper.insertZknjFavourite(zknjFavourite);
    }

    /**
     * 修改收藏夹
     * 
     * @param zknjFavourite 收藏夹
     * @return 结果
     */
    @Override
    public int updateZknjFavourite(ZknjFavourite zknjFavourite)
    {
        zknjFavourite.setUpdateTime(DateUtils.getNowDate());
        return zknjFavouriteMapper.updateZknjFavourite(zknjFavourite);
    }

    /**
     * 批量删除收藏夹
     * 
     * @param ids 需要删除的收藏夹主键
     * @return 结果
     */
    @Override
    public int deleteZknjFavouriteByIds(String ids)
    {
        return zknjFavouriteMapper.deleteZknjFavouriteByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除收藏夹信息
     * 
     * @param id 收藏夹主键
     * @return 结果
     */
    @Override
    public int deleteZknjFavouriteById(Long id)
    {
        return zknjFavouriteMapper.deleteZknjFavouriteById(id);
    }

    /**
     * 通过Request添加收藏夹信息
     * @param zknjRequest
     * @return
     */
    @Override
    public int insertByRequest(ZknjRequest zknjRequest) {
        String favName = "favourite_"+DateUtils.getNowDate().getTime();
        zknjRequest.getSamples().forEach(sample -> {
            ZknjFavourite favourite = new ZknjFavourite();
            favourite.setFavName(favName);
            favourite.setSamName(sample.getSamName());
            favourite.setSamDes(sample.getSamDes());
            favourite.setmFormula(sample.getmFormula());
            favourite.setmWeight(sample.getmWeight());
            favourite.setRisks(sample.getRisks());
            favourite.setMethods(sample.getModIds());
            favourite.setServices(getServicesFromModIds(sample.getModIds()));
            favourite.setCreateBy(ShiroUtils.getLoginName());
            favourite.setCreateTime(DateUtils.getNowDate());
            zknjFavouriteMapper.insertZknjFavourite(favourite);
        });
        return 1;
    }

    /**
     * 由方法获取服务的ids
     * @param modIds
     * @return
     */
    private String getServicesFromModIds(String modIds) {
        String[] split = modIds.split(",");
        List<Long> serIds = new ArrayList<>();
        if(split.length == 1){
            Long modId = Long.valueOf(modIds.trim());
            serIds.add(methodMapper.selectZknjMethodByModId(modId).getSerId());
        }else {
            for (String s : split) {
                Long modId = Long.valueOf(s.trim());
                serIds.add(methodMapper.selectZknjMethodByModId(modId).getSerId());
            }
        }
        return StringUtils.join(serIds, ",");
    }
}
